package cn.tedu.charging.order.mqtt;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class MqttProducer {
    @Autowired
    private MqttClient mqttClient;
    public Boolean send(String topic, String message) {
        try {
            // 发布消息
            mqttClient.publish(topic, message.getBytes(), 0, false);//message.getBytes()是字节数组，用于存放消息[  String-->字节[]  ]
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("给EMQX发送消息失败，Topic:{},消息:{}",topic,message,e);
        }
        return false;
    }
}
